from selenium import webdriver
from selenium.webdriver.common.by import By
import time


class Send_mail:
    def __init__(self):
        # 打开浏览器，请求目标网址
        self.driver = webdriver.Chrome()
        self.driver.get('https://mail.163.com/')

    def login_163(self):
        # # 打开浏览器，请求目标网址
        # driver = webdriver.Chrome()
        # driver.get('https://mail.163.com/')

        # 添加登录需要的cookie
        cookies = [
            {
                'name': 'NTES_SESS',
                'value': 'Qw7y39zoJVEEtM5rWGeN82H8U2wgO_b7gtqBs1XI7oz_a7s5atXR4y.576VcSWmdWic5Oe9fcQvb4INCMUQ8z57TywU4b447oog7uMPcQIeBhWZkz9N.9uVqcBtmuoy3G.vSFkHS2.36l0.gSnJV.z3I5xmpzMVGBmdjHFnGKNOxZJz_v2j9kkIDlOYro148dQM0rmxox53GKizfDF4PKIkZh'
            },
            {
                'name': 'MAIL_ENTRY_INFO',
                'value': '1|0|mail163|email163|221.234.129.46|d3c7cfc5e9025a6b299737db562996ad_v1|'
            },
            {
                'name': 'MAIL_ENTRY_CS',
                'value': '6735709c40495f34a4f8fcf52e02dcf6'
            },
            {
                'name': 'Coremail',
                'value': '270fa567af3cf%bDdaZHbZJaFbITgKUQZZubQuRbSpkCHi%g1a153.mail.163.com'
            },
            {
                'name': 'cm_last_info',
                'value': 'dT1oangxOTc1NTAxMjgyJTQwMTYzLmNvbSZkPWh0dHBzJTNBJTJGJTJGbWFpbC4xNjMuY29tJTJGanM2JTJGbWFpbi5qc3AlM0ZzaWQlM0RiRGRhWkhiWkphRmJJVGdLVVFaWnViUXVSYlNwa0NIaSZzPWJEZGFaSGJaSmFGYklUZ0tVUVpadWJRdVJiU3BrQ0hpJmg9aHR0cHMlM0ElMkYlMkZtYWlsLjE2My5jb20lMkZqczYlMkZtYWluLmpzcCUzRnNpZCUzRGJEZGFaSGJaSmFGYklUZ0tVUVpadWJRdVJiU3BrQ0hpJnc9aHR0cHMlM0ElMkYlMkZtYWlsLjE2My5jb20mbD0tMSZ0PTcmYXM9dHJ1ZQ=='
            }
        ]

        # 添加cookie
        self.driver.add_cookie(cookies[0])
        self.driver.add_cookie(cookies[1])
        self.driver.add_cookie(cookies[2])
        self.driver.add_cookie(cookies[3])
        self.driver.add_cookie(cookies[4])

        # 刷新浏览器
        self.driver.refresh()

    def send_mail(self):
        # 定位写信并点击
        self.driver.find_element(By.ID, '_mail_component_149_149').click()
        # 定位给自己写一封信按钮
        # btn1=self.driver.find_element(By.XPATH,'//*[@id="1652701848765_list"]/div/div[1]/ul/li/a')
        # btn1.click()

        write = self.driver.find_element(By.ID, '_mail_emailcontainer_0_498')
        write.send_keys('hjx1975501282@163.com')

    def quit(self):
        time.sleep(5)
        self.driver.quit()

if __name__ == '__main__':
    send = Send_mail()
    send.login_163()
    # send.send_mail()
    # send.quit()
